package util

import (
	"net/http"
	"strings"
	"os/exec"
	"gitee.com/DengAnbang/Computation/src/code"
	"path"
	"gitee.com/DengAnbang/goutils/fileUtil"
	"errors"
)

func GetValueFormRequest(r *http.Request, key string) string {
	ct := r.Header.Get("Content-Type")
	if strings.Contains(ct, "multipart/form-data") {
		r.ParseMultipartForm(32 << 20)
		if r.MultipartForm != nil {
			values := r.MultipartForm.Value[key]
			if len(values) > 0 {
				return values[0]
			}
		}
	} else {
		r.ParseForm()
		return r.Form.Get(key)
	}
	return ""
}
func ExtractDb(filePath string) error {
	cmd := exec.Command("java", "-jar", code.ConfigRootPath+code.GetB3dDBJar, code.ConfigRootPath+code.GetB3dDbDLL, filePath)
	_, err := cmd.Output()
	if err != nil {
		return err
	}

	fileSuffix := path.Ext(filePath)
	filenameOnly := strings.TrimSuffix(filePath, fileSuffix)
	if fileUtil.PathExists(filenameOnly + ".db") {
		return nil
	} else {
		return errors.New("db文件解析失败")
	}
}
